package com.qf.application.service.impl;

import com.qf.application.commons.exception.BusinessException;
import com.qf.application.commons.utils.R;
import com.qf.application.entity.User;
import com.qf.application.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Map;

@Service
public class LoginServiceImpl {

    @Autowired
    private IUserService userService;

    public User login(Map<String, String> map) {

        String username = map.get("username");
        String password = map.get("password");

        // 1.非空判断
        if (ObjectUtils.isEmpty(username) || ObjectUtils.isEmpty(password)) {
            throw new BusinessException(3001, "用户名获密码为空");
        }

        // 2.根据用户名查询用户的信息
        User user = userService.getUserByUsername(username);

        // 3.判断用户是否存在
        if (user == null) {
            throw new BusinessException(3001, "用户名不存在");
        }

        // 4.密码的对比
        if (!user.getPassword().equals(password)) { // 不做加密
            throw new BusinessException(3001, "用户名或密码错误");
        }

        // 为了安全起见把密码设置为null
        user.setPassword(null);
        return user;
    }
}
